package nhatquang.gs.model;

import nhatquang.gs.controler.FruitManager;
import nhatquang.gs.controler.ResourcesManager;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.vbo.ITiledSpriteVertexBufferObject;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

public class MyAnimatedModifier extends AnimatedSprite {
	IAnimationListener animationListener;
	private boolean end;
	Scene scene;
//	private long frameDuration[] = { 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
//			20, 20 };

	public MyAnimatedModifier(float pX, float pY, float pWidth, float pHeight,
			ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager vBOM,
			boolean end, Scene scene) {
		super(pX, pY, pWidth, pHeight, pTiledTextureRegion,
				vBOM);
		this.end = end;
		this.scene = scene;
		animationListener = new IAnimationListener() {

			@Override
			public void onAnimationStarted(AnimatedSprite pAnimatedSprite,
					int pInitialLoopCount) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite,
					int pRemainingLoopCount, int pInitialLoopCount) {

			}

			@Override
			public void onAnimationFrameChanged(AnimatedSprite pAnimatedSprite,
					int pOldFrameIndex, int pNewFrameIndex) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationFinished(AnimatedSprite pAnimatedSprite) {
				// TODO Auto-generated method stub
//				detack();
			}
		};
		this.animate(20, false, animationListener);

		scene.attachChild(this);
		// TODO Auto-generated constructor stub
	}

	public void detack() {
		if (this.end) {
			ResourcesManager.INSTANCE.mActivity.runOnUpdateThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
//					scene.detachChild(MyAnimatedModifier.this);
				}
			});
			
			FruitManager.INSTANCE.updateArray();
			return;
		}
//		scene.detachChild(this);
	}

}
